<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .area1 {
      width: 520px;
      height: 510px;
    }

    .grid {
      border-bottom: 1px solid #333;
      padding: 0;
    }

    .grid li {
      list-style: none;
      height: 100px;
      border-top: 1px solid #333;
      border-left: 1px solid #333;
      border-right: 1px solid #333;
    }

    .cell {
      display: inline-block;
      width: 100px;
      height: 100px;
      border-right: 1px solid #333;
    }

    .box {
      position: absolute;
      width: 100px;
      height: 100px;
      cursor: move;
    }

    #timo {
      background-image: url('./img/timo4.png');
      left: 0px;
    }

    #lulu {
      background-image: url('./img/lulu.png');
      left: 120px;
    }

    #kainan {
      background-image: url('./img/kainan.png');
      left: 240px;
    }
  </style>
</head>

<body>
  <div class="area1">
    <ul class="grid">
      <li>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
      </li>
      <li>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
      </li>
      <li>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
      </li>
      <li>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
      </li>
      <li>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
        <span class="cell"></span>
      </li>
    </ul>
  </div>
  <div class="area2">
    <div class="box" id="timo"></div>
    <div class="box" id="lulu"></div>
    <div class="box" id="kainan"></div>
  </div>
  <script>
    window.onload = function () {
      var dragItems = document.querySelectorAll('.box');
      // //点击某物体时，用drag对象即可，move和up是全局区域，
      // 也就是整个文档通用，应该使用document对象而不是drag对象(否则，采用drag对象时物体只能往右方或下方移动)  
      dragItems.forEach(drag => {

        drag.onmousedown = function (event) {
          var event = event || window.event;  //兼容IE浏览器
          //    鼠标点击物体那一刻相对于物体左侧边框的距离=点击时的位置相对于浏览器最左边的距离-物体左边框相对于浏览器最左边的距离
          var diffX = event.clientX - drag.offsetLeft;
          var diffY = event.clientY - drag.offsetTop;
          if (typeof drag.setCapture !== 'undefined') {
            drag.setCapture();
          }
          document.onmousemove = function (event) {
            var event = event || window.event;
            var moveX = event.clientX - diffX;
            var moveY = event.clientY - diffY;
            if (moveX < 0) {
              moveX = 0
            } else if (moveX > window.innerWidth - drag.offsetWidth) {
              moveX = window.innerWidth - drag.offsetWidth
            }
            if (moveY < 0) {
              moveY = 0
            } else if (moveY > window.innerHeight - drag.offsetHeight) {
              moveY = window.innerHeight - drag.offsetHeight
            }
            drag.style.left = moveX + 'px';
            drag.style.top = moveY + 'px'
          }
          document.onmouseup = function (event) {
            this.onmousemove = null;
            this.onmouseup = null;
            //修复低版本ie bug  
            if (typeof drag.releaseCapture != 'undefined') {
              drag.releaseCapture();
            }
          }
        }
      })
    }
  </script>
</body>

</html>